Map<int, String> weekdayDict = {
  1: "一",
  2: "二",
  3: "三",
  4: "四",
  5: "五",
  6: "六",
  7: "日"
};

class DateUtilsExt {
  static String toIdiomaticFormat(DateTime dt, bool needTime) {
    DateTime now = DateTime.now();
    DateTime dayStart = DateTime(dt.year, dt.month, dt.day);

    DateTime dayNow = DateTime(now.year, now.month, now.day);
    int days = dayNow.difference(dayStart).inDays;

    String day = "${dt.day}";
    // dt.day < 10 ? "0${dt.day}" : "${dt.day}";
    int dtYear = dt.year;
    String dtMouth =
        "${dt.month}"; // dt.month < 10 ? "0${dt.month}" : "${dt.month}";

    String hour = "${dt.hour}";
    // dt.hour < 10 ? "0${dt.hour}" : "${dt.hour}";
    String min =
        "${dt.minute}"; // dt.minute < 10 ? "0${dt.minute}" : "${dt.minute}";
    min = min.padLeft(2, "0");
    late String result;

    if (days == 0) {
      result = "$hour:$min";
    } else if (days == 1) {
      result = "昨天 $hour:$min";
    } else if (days == 2) {
      result = "前天 $hour:$min";
    } else if (days > 2 && days < 7) {
      result = "周${weekdayDict[dt.weekday]}";
      if (needTime) {
        result = "$result $hour:$min";
      }
    } else {
      if (dtYear == now.year) {
        result = "$dtMouth-$day ";
      } else {
        result = "$dtYear-$dtMouth-$day ";
      }
      if (needTime) {
        result = "$result $hour:$min";
      }
    }

    return result;
  }

  static String toStandardStr(DateTime dt) {
    return "${dt.year}-${dt.month}-${dt.day} ${dt.hour}:${dt.minute}:${dt.second}";
  }

  static String toPersonalizedStr(DateTime dt) {
    return "${dt.year}-${dt.month}-${dt.day}";
  }

  static bool isYesterday(DateTime dt) {
    int dtLong = dt.millisecondsSinceEpoch;
    DateTime now = DateTime.now();

    DateTime nowStartTime = DateTime.parse(
        "${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} 00:00:00");

    int nowStartTimeMilliseconds = nowStartTime.millisecondsSinceEpoch;

    double days = (nowStartTimeMilliseconds - dtLong) / 86400000;
    return (days > 0 && days <= 24);
  }
}

///简化日期，其目的只要是为了增加可读性，减少显示日期时所占的屏幕空间，只支持yyyy-MM-dd格式
///比如2023-02-22简化为2-22，如果是去年的日期，比如2022-02-09，则简化为22-2-9
String simplizeDateStr(String dateStr) {
  List<String> parts = dateStr.split("-");
  if (parts.length < 3) {
    return dateStr;
  }
  int year = int.parse(parts[0]);
  int month = int.parse(parts[1]);
  int day = int.parse(parts[2]);

  DateTime now = DateTime.now();
  if (year != now.year) {
    return "${year.toString().substring(2)}-$month-$day";
  } else {
    return "$month-$day";
  }
}

///yyyy-M-d h(h):m(m):s(s),yyyy-MM-dd h(h):m(m):s(s),yyyy/MM/dd h(h):m(m):s(s),yyyy/M/d h(h):m(m):s(s)
///yyyyMMDD h(h):m(m):s(s)
DateTime? stringToDate(String value) {
  String? standardStr = dateStrToStandardStr(value);
  if (standardStr != null) {
    return DateTime.tryParse(standardStr);
  }
  return null;
}

String? dateStrToStandardStr(String value) {
  DateTime? dt = DateTime.tryParse(value);
  String standardStr = value.replaceAll("/", "-");
  if (dt != null && standardStr.contains("-")) {
    return value;
  }

  value = value.trim();

  List<String> parts = standardStr.split(" ");
  String datePart = parts[0];
  //yyyyMMdd
  if (datePart.length == 8 && !datePart.contains("-")) {
    String yearStr = datePart.substring(0, 4);
    String monthStr = datePart.substring(4, 6);
    String dayStr = datePart.substring(6, 8);
    datePart = "$yearStr-$monthStr-$dayStr";
  } else {
    List<String> ymd = datePart.split("-");
    if (ymd.length == 3) {
      String yearStr = ymd[0];
      String monthStr = ymd[1];
      String dayStr = ymd[2];
      if ((yearStr.isNotEmpty && yearStr.length == 4 && yearStr != "0000") &&
          (monthStr.isNotEmpty &&
              monthStr.length <= 2 &&
              monthStr != "0" &&
              monthStr != "00") &&
          (dayStr.isNotEmpty &&
              dayStr.length <= 2 &&
              dayStr != "0" &&
              dayStr != "00")) {
        datePart =
            "$yearStr-${monthStr.padLeft(2, '0')}-${dayStr.padLeft(2, '0')}";
      }
    } else {
      return null;
    }
  }

  //With time
  if (parts.length > 1 && parts[1].isNotEmpty) {
    String timePart = parts[parts.length - 1];
    List<String> timeParts = timePart.split(":");
    if (timeParts.length == 1) {
      timePart = "${timeParts[0]}:00:00";
    } else if (timeParts.length == 2) {
      timePart =
          "${timeParts[0].padLeft(2, "0")}:${timeParts[1].padLeft(2, "0")}:00";
    } else if (timeParts.length == 3) {
      timePart =
          "${timeParts[0].padLeft(2, "0")}:${timeParts[1].padLeft(2, "0")}:${timeParts[2].padLeft(2, "0")}";
    }

    return "$datePart $timePart";
  }
  return datePart;
}

bool isEqualsWithStandardStr(DateTime dt, String standardStr) {
  List<String> parts = standardStr.split(" ");
  String datePart = parts[0];
  List<String> ymdParts = datePart.split("-");

  bool result = (dt.year == int.parse(ymdParts[0]) &&
      dt.month == int.parse(ymdParts[1]) &&
      dt.day == int.parse(ymdParts[2]));
  if (result == false) return false;

  //With time
  if (parts.length > 1) {
    String timePart = parts[1];
    List<String> hmsParts = timePart.split(":");
    result = result &&
        (dt.hour == int.parse(hmsParts[0]) &&
            dt.minute == int.parse(hmsParts[1]) &&
            dt.second == int.parse(hmsParts[2]));
  }
  return result;
}
